package com.daocao.jy.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.daocao.jy.R;
import com.daocao.jy.adapter.MyCouponAdapter;
import com.daocao.jy.api.Api;
import com.daocao.jy.api.bean.CouponListBean;
import com.daocao.jy.util.Network;
import com.daocao.jy.util.RxLifeUtil;
import com.daocao.jy.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-4-10.
 */

public class MyCouponFragment extends BaseFragment {
    @BindView(R.id.list)
    RecyclerView list;
    Unbinder unbinder;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    private MyCouponAdapter adapter;
    private int id;
    private static final String STATUS_USE="1";
    private static final String STATUS_USED="2";
    private static final String STATUS_TIMEOUT="0";
    @Override
    protected int setLayoutId() {
        return R.layout.fragment_my_coupon;
    }

    //    "可使用", "已使用", "已过期"
    @Override
    protected void initView() {
        if (getArguments() != null) {
            id = getArguments().getInt("id", 0);
        }
        adapter = new MyCouponAdapter(getContext(), id);
        list.setAdapter(adapter);

        switch (id){
            case 0:
                getCouponList(STATUS_USE);
                break;
            case 1:
                getCouponList(STATUS_USED);
                break;
            case 2:
                getCouponList(STATUS_TIMEOUT);
                break;
        }
    }

    public void getCouponList(String status) {
//        "可使用", "已使用", "已过期"
        //状态（0：过期，1:有效，2：已使用)
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }
        CouponListBean bean = new CouponListBean();
        bean.status = status;
        Api.api_service.getCouponList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {

                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (result.datas != null && result.datas.size() != 0) {
                        adapter.update(result.datas);
                        layoutEmpty.setVisibility(View.GONE);
                    } else {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    }

                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
